package com.newshub.app.ui.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.ImageButton
import android.widget.TextView
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.newshub.app.R
import com.newshub.app.data.model.News
import java.text.SimpleDateFormat
import java.util.*

class NewsAdapter(
    private val onItemClick: (News) -> Unit,
    private val onFavoriteClick: (News) -> Unit = {}
) : ListAdapter<News, NewsAdapter.NewsViewHolder>(NewsDiffCallback()) {
    
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NewsViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_news, parent, false)
        return NewsViewHolder(view, onItemClick, onFavoriteClick)
    }
    
    override fun onBindViewHolder(holder: NewsViewHolder, position: Int) {
        holder.bind(getItem(position))
    }
    
    class NewsViewHolder(
        itemView: View,
        private val onItemClick: (News) -> Unit,
        private val onFavoriteClick: (News) -> Unit
    ) : RecyclerView.ViewHolder(itemView) {
        
        private val titleText: TextView by lazy { itemView.findViewById(R.id.titleText) ?: TextView(itemView.context) }
        private val digestText: TextView by lazy { itemView.findViewById(R.id.digestText) ?: TextView(itemView.context) }
        private val sourceText: TextView by lazy { itemView.findViewById(R.id.sourceText) ?: TextView(itemView.context) }
        private val timeText: TextView by lazy { itemView.findViewById(R.id.timeText) ?: TextView(itemView.context) }
        private val favoriteButton: ImageButton by lazy { itemView.findViewById(R.id.favoriteButton) ?: ImageButton(itemView.context) }
        private val imageView: ImageView by lazy { itemView.findViewById(R.id.imageView) ?: ImageView(itemView.context) }
        
        fun bind(news: News) {
            titleText.text = news.title
            digestText.text = news.digest
            sourceText.text = news.source
            timeText.text = formatTime(news.ptime)
            
            // 设置收藏按钮状态
            favoriteButton.setImageResource(
                if (news.isFavorite) android.R.drawable.btn_star_big_on
                else android.R.drawable.btn_star_big_off
            )
            
            // 加载图片
            news.imgsrc?.let {
                if (it.isNotEmpty()) {
                    Glide.with(imageView.context)
                        .load(it)
                        .centerCrop()
                        .into(imageView)
                }
            }
            
            // 设置点击事件
            itemView.setOnClickListener {
                onItemClick(news)
            }
            
            favoriteButton.setOnClickListener {
                onFavoriteClick(news)
            }
        }
        
        private fun formatTime(timeStr: String): String {
            return try {
                val inputFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault())
                val outputFormat = SimpleDateFormat("MM-dd HH:mm", Locale.getDefault())
                val date = inputFormat.parse(timeStr)
                outputFormat.format(date ?: Date())
            } catch (e: Exception) {
                timeStr
            }
        }
    }
    
    class NewsDiffCallback : DiffUtil.ItemCallback<News>() {
        override fun areItemsTheSame(oldItem: News, newItem: News): Boolean {
            return oldItem.docid == newItem.docid
        }
        
        override fun areContentsTheSame(oldItem: News, newItem: News): Boolean {
            return oldItem == newItem
        }
    }
}
